package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Override
    /**
     * 新增菜品套餐
     */
    @Transactional
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //调用方法存入setmeal表
        setmealMapper.inster(setmeal);
        //存入setmealdish表
        //获取insert语句生成的主键
        Long id = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && setmealDishes.size() > 0) {
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(id);
                //向口味表插入n条数据
                setmealDishMapper.insertBatch(setmealDish);

            }

        }
    }

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        //调用工具完成分页
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        //调用持久层方法完成查询
        Page<Setmeal> page = setmealMapper.select(setmealPageQueryDTO);
        PageResult pageResult = new PageResult(page.getTotal(), page.getResult());
        return pageResult;
    }

    /**
     * 套餐修改
     * @param setmealDTO
     * @param
     */
    @Transactional//开启事务
    @Override
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        Long id = setmealDTO.getId();
        //调用方法修改表数据
        setmeal.setId(id);
        setmealMapper.update(setmeal);
        //删除套餐内菜品表的数据
        setmealDishMapper.delect(id);
        //插入新的菜品表
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && setmealDishes.size() > 0) {
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(id);
                //向口味表插入n条数据
                setmealDishMapper.insertBatch(setmealDish);
            }
        }
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public SetmealVO selectById(Long id) {
        SetmealVO setmealVO = new SetmealVO();
        //调用方法获得套餐数据
        Setmeal setmeal= setmealMapper.getById(id);
        //将套餐数据copy给返回
        BeanUtils.copyProperties(setmeal,setmealVO);
        List<SetmealDish> setmealDishes = setmealDishMapper.getById(id);
        setmealVO.setSetmealDishes(setmealDishes);

        return setmealVO;
    }

    /**
     * 批量删除
     * @param ids
     */
    @Transactional
    @Override
    public void deleteById(List<Long> ids) {

//循环遍历ids,依次删除
        for (Long id : ids) {
            setmealMapper.deleteByIds(id);
            setmealDishMapper.delect(id);
        }
    }

    /**
     * 套餐起售、停售
     * @param status
     * @param id
     */
    @Override
    public void updateStatusById(Integer status, Long id) {
        //根据id获得需要修改的数据
        Setmeal setmeal = setmealMapper.getById(id);
        //修改状态
        setmeal.setStatus(status);
        setmealMapper.update(setmeal);
    }

    /**

     - 条件查询
     - @param setmeal
     - @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**

     - 根据id查询菜品选项
     - @param id
     - @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}



